<template>
	<view>
		<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			<image class="avatar" :src="user.avatar?user.avatar: imgUrl+'index/33.png'"></image>
		</button>
		<view class="user">点击获取/更新用户头像</view>
		<view class="input-view display-flex">
			<label>昵称</label>
			<view>
				<input type="nickname" style="width: 500rpx;" placeholder="请输入昵称或者直接获取微信昵称" v-model="user.nickname"
					@blur="onBlru" placeholder-class="font-size_28rpx" />
			</view>
		</view>

		<block v-if="type == 1">
			<view class="pick-button" @click="auth()">登录</view>
			<view class="cancel" @click="close()">取消</view>
		</block>
		<block v-if="type == 2">
			<view class="pick-button" @click="sub()">确认更新</view>
		</block>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					id: '',
					avatar: '',
					nickname: '',
				},
				storageMode: ''
				imgUrl: this.$imgUrl,

				system: uni.getStorageSync("system"),

				type: '', //1用户授权  2修改用户信息

				flag: false,
			}
		},

		onUnload() { //监听是否跳转页面
			if (this.flag) {
				//tjzt为变量
			} else {
				uni.navigateBack({
					delta: 2
				})
			}

		},

		onLoad(options) {
			if (options.type) {
				this.type = options.type;
				if (options.type == 1) {
					this.login();
					this.$sun.title("授权登录");
				}
				if (options.type == 2) {
					this.userInfo();
					this.$sun.title("更新头像昵称");
				}
			}
		},

		onShow() {

		},

		methods: {

			//用户信息
			async userInfo() {
				const result = await this.$http.post({
					url: this.$api.userInfo,
					data: {
						uid: uni.getStorageSync('userId')
					}
				});
				if (result.errno == 0) {
					this.user = result.data;
				}
			},

			login() {
				uni.login({
					provider: 'weixin',
					success: async res => {
						const result = await this.$http.post({
							url: this.$api.getOpenId,
							data: {
								code: res.code
							}
						});
						if (result.errno == 0) {
							uni.setStorageSync('openid', result.data.openid);
							uni.setStorageSync('session_key', result.data.session_key);
						} else {
							this.$sun.toast(result.message, 'none');
						}
					}
				});
			},

			onBlru(e) {
				this.user.nickname = e.detail.value;
			},
			async getUploadConfig() {
				const result = await this.$http.get({
					url: this.$api.getStorageConfig,
					data: {}
				});
				if (result.code == 200) {
					this.storageMode = result.data.storageMode; // storageMode:2是‘阿里云’上传，3是‘七牛云’ 上传
					console.log('this.storageMode', this.storageMode);

				} else {
					console.log('result.message', result.message);
				}
			},
			onChooseAvatar(e) {
				// this.user.avatar = e.detail.avatarUrl;
				// uni.uploadFile({
				// 	url: this.$api.upload, //后台接口
				// 	filePath: e.detail.avatarUrl, // 上传图片 url
				// 	name: 'file',
				// 	success: res => {
				// 		res = JSON.parse(res.data);

				// 		console.log('uploadRes', res);

				// 		if (res.errno != 0) {
				// 			uni.showToast({
				// 				title: '上传失败 : ' + res.data,
				// 				icon: 'none'
				// 			});
				// 		} else {
				// 			//上传图片成功
				// 			this.user.avatar = res.data;
				// 		}
				// 	},
				// 	fail: e => {
				// 		this.$toast('上传失败')
				// 	}
				// });
			},

			close() {
				uni.navigateBack({
					delta: 2
				})
			},

			//登录授权
			auth() {

				if (!this.user.avatar) {
					this.$sun.toast("请上传头像", "error");
					return;
				}
				if (!this.user.nickname) {
					this.$sun.toast("请输入名称", "error");
					return;
				}

				uni.request({
					url: this.$api.register,
					method: "POST",
					header: {
						"Content-type": "application/json"
					},
					data: {
						pid: uni.getStorageSync('pid'),
						openid: uni.getStorageSync('openid'),
						avatar: this.user.avatar,
						nickname: this.user.nickname,
					},
					success: res => {
						console.log('成功', res);
						if (res.errMsg == "request:ok") {
							uni.setStorageSync("uid", res.data.data);
							uni.showToast({
								title: '登录成功',
								duration: 1500,
							});
							setTimeout(() => {
								this.flag = true;
								uni.navigateBack();
							}, 1500);
						}
					},
					fail: (err) => {
						console.log('失败', err);
					},
					complete: (res) => {
						console.log('完成', res);
						// uni.setStorageSync('userInfo', userDetailInfo);
					}
				});
			},

			/*  更新  */
			async sub() {

				let flag = true;
				if (!this.user.avatar) {
					this.$sun.toast("请上传头像", "error");
					flag = false;
					return;
				}
				if (!this.user.nickname) {
					this.$sun.toast("请输入名称", "error");
					flag = false;
					return;
				}


				if (flag) {

					const result = await this.$http.post({
						url: this.$api.register,
						data: {
							openid: uni.getStorageSync('openid'),
							nickname: this.user.nickname,
							avatar: this.user.avatar,
						}
					});
					if (result.errno == 0) {
						uni.showToast({
							title: '更新成功',
							duration: 1500,
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					} else {
						this.$sun.toast(result.message, "none");
					}
				}
			},


		}
	}
</script>

<style lang="scss">
	.bott {
		position: fixed;
		bottom: 0;
		width: 750rpx;
	}

	.user {
		text-align: center;
		color: #999999;
		font-size: 26rpx;
		margin-bottom: 60rpx;
	}

	page {
		background-color: #FFFFFF;
	}

	.avatar-wrapper {
		background-color: #FFFFFF;
		padding: 80rpx 0 20rpx;
	}

	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}

	.input-view {
		align-items: center;
		padding: 30rpx;
		border-bottom: 2rpx solid #F7F7F7;

		>label {
			width: 168rpx;
			text-align: center;
		}

	}

	.cancel {
		width: 360rpx;
		background-color: #f7f7f7;
		color: #E53A3A;
		border-radius: 10rpx;
		text-align: center;
		font-size: 30rpx;
		padding: 20rpx;
		margin: 0 195rpx;
	}

	.pick-button {
		width: 360rpx;
		background-color: #17B10A;
		color: #ffffff;
		border-radius: 10rpx;
		text-align: center;
		font-size: 30rpx;
		padding: 20rpx;
		margin: 60rpx 195rpx 30rpx;
	}
</style>